package com.tongguan.paypos.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tongguan.paypos.R;
import com.tongguan.paypos.common.AppConstants;
import com.tongguan.paypos.entity.BillsInfo;
import com.tongguan.paypos.utils.Arith;

import java.util.ArrayList;

/**
 * Created by Joylive on 2016/12/17 .
 */

public class TransBillsAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<BillsInfo> mDatas;
    private double money;//实付金额
    private double coupon_money;//优惠金额
    private double amount;//总金额
    private boolean isCoupon = false;//是否优惠

    public TransBillsAdapter(Context context, ArrayList<BillsInfo> datas) {
        this.context = context;
        this.mDatas = datas;
    }

    @Override
    public int getCount() {
        if (mDatas != null && mDatas.size() > 0)
            return mDatas.size();
        else
            return 0;
    }

    @Override
    public BillsInfo getItem(int position) {
        if (mDatas != null && mDatas.size() > 0)
            return mDatas.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_view_tans, null);
            holder.mTransPrice = (TextView) convertView.findViewById(R.id.trans_price);
            holder.mTransState = (TextView) convertView.findViewById(R.id.trans_state);
            holder.mTransCardNo = (TextView) convertView.findViewById(R.id.trans_card_no);
            holder.mTransDate = (TextView) convertView.findViewById(R.id.trans_date);
            holder.mTransTime = (TextView) convertView.findViewById(R.id.trans_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BillsInfo billsInfo = mDatas.get(position);
        money = billsInfo.getPayMoney();
        coupon_money = billsInfo.getCouponMoney();
        if (coupon_money > 0) {
            isCoupon = true;
            amount = Arith.sub(money, coupon_money);
        } else {
            isCoupon = false;
            amount = money;
        }
        holder.mTransPrice.setText(String.valueOf(amount));
        setOrderStateColor(billsInfo.getState(), holder.mTransState);
        String payTypeText = AppConstants.getPayTypeText(billsInfo.getChannelId());
        if (isCoupon) {
            holder.mTransCardNo.setText(payTypeText + " 优惠 -"+coupon_money);
        }else {
            holder.mTransCardNo.setText(payTypeText);
        }
        String dateTime = billsInfo.getPayTime();
        int length = dateTime.length();
        if (!TextUtils.isEmpty(dateTime) && length > 8) {
            holder.mTransDate.setText(dateTime.substring(0, length - 8));
            holder.mTransTime.setText(dateTime.substring(length - 8, length));
        }
        return convertView;
    }

    static class ViewHolder {
        TextView mTransPrice;
        TextView mTransState;
        TextView mTransCardNo;
        TextView mTransDate;
        TextView mTransTime;
    }

    private void setOrderStateColor(String type, TextView textView) {
        switch (type) {
            case "0":
                textView.setText(AppConstants.orderState.get(type));
                textView.setTextColor(context.getResources().getColor(R.color.theme));
                break;
            case "2":
                textView.setText(AppConstants.orderState.get(type));
                textView.setTextColor(context.getResources().getColor(R.color.red_color_revoke));
                break;
            case "3":
                textView.setText(AppConstants.orderState.get(type));
                textView.setTextColor(context.getResources().getColor(R.color.red_color_reversal));
                break;
            default:
                textView.setText(AppConstants.orderState.get(type));
                textView.setTextColor(context.getResources().getColor(R.color.theme));
                break;
        }
    }
}

